<?php
/**
 * This file is part of Project.
 *
 * @link     https://www.xxx.com
 * @contact  xxxx@xxx.com
 */

namespace App\Repository;


use App\Model\MediaModel;

class MediaRepository extends BaseRepository
{
    public function __construct()
    {
        $this->model = new MediaModel();
    }

    /**
     * Notes: 返回列表
     * Author: Bruce.z
     * DateTime: 2022/1/4 16:04
     * @param array $condition
     * @return array
     */
    public function index(array $condition) : array
    {
        $size = $condition['size'] ?? 10;
        $where['catalog_id'] = $condition['pid'] ?: 0;
        $pid  = $condition['pid']??0;

        $dir_list = CatalogRepository::getInstance()->getList($pid, $condition['page'], $size);
        $dir_count = CatalogRepository::getInstance()->getCount($pid);

        $img_list = [];
        if($dir_count < $condition['page'] * $size){
            if($dir_count == 0){
                $_offset = ($condition['page'] - 1) * $size;
            }else{
                $cc = $dir_count<=$size ? $size-$dir_count :  $size-$dir_count%$size;
                $_offset = $condition['page'] <= ceil($dir_count/$size) ? 0 :
                    ($cc + ($condition['page'] - 2 - floor($dir_count/$size))*$size);
            }
            $img_list = $this->_searchFactor($where)
                ->offset($_offset)
                ->limit($size-count($dir_list))
                ->get()
                ->toArray();
        }

        $count = $this->_searchFactor($where)->count();

        return ['total'=>$count, 'data'=>['dir_list'=>$dir_list,'img_list'=>$img_list]];
    }
}
